# 导入selenium的webdriver接口
import time
import pandas as pd

from selenium import webdriver

movie_id_list = []
movie_name_list = []
movie_star_list = []
movie_time_list = []
score_list  = []


def get_one_page_data(b):
    dd_list = b.find_elements_by_xpath('//dl[@class="board-wrapper"]/dd')
    for dd in dd_list:
        movie_id = dd.find_element_by_xpath('./i').text
        movie_id_list.append(movie_id)
        movie_name = dd.find_element_by_xpath('./div/div/div[1]/p[@class="name"]/a').text
        movie_name_list.append(movie_name)
        movie_star = dd.find_element_by_xpath('./div/div/div[1]/p[@class="star"]').text
        movie_star_list.append(movie_star)
        movie_time = dd.find_element_by_xpath('./div/div/div[1]/p[@class="releasetime"]').text
        movie_time_list.append(movie_time)
        i_list = dd.find_elements_by_xpath('./div/div/div[2]/p/i')
        print(i_list)
        score = ''
        for i in i_list:
            score += str(i.text).replace("\n", "").replace("\t", "").replace(" ", "")
        score_list.append(score)
        print(f"排名:{movie_id}, 电影名称:{movie_name}, 主演:{movie_star}, 上映时间:{movie_time}, 评分:{score}")
        print("------------------------------------------------")


if __name__ == '__main__':

    # options = webdriver.ChromeOptions()
    # options.add_argument('--headless')
    # # 1、创建浏览器对象 - 打开浏览器
    # # driver = webdriver.Chrome()
    # browser = webdriver.Chrome(chrome_options=options)  # 创建一个隐藏浏览器


    # 1.创建浏览器对象 - 此时浏览器打开
    browser = webdriver.Chrome()

    browser.get("https://www.maoyan.com/board/4?offset=0")
    time.sleep(2)

    page_num = 1
    while True:
        # 获取当前页的数据
        get_one_page_data(browser)
        next_page = browser.find_element_by_xpath('//ul[@class="list-pager"]/li[last()]/a')
        if next_page.text!='下一页':
            print("爬虫结束！")
            break
        # 点击一下，进行下一页
        next_page.click()
        time.sleep(1)

    info_dict = {
        '排名': movie_id_list,
        '电影名称': movie_name_list,
        '主演': movie_star_list,
        '上映时间': movie_time_list,
        '评分': score_list
    }

    pd.DataFrame(info_dict).to_csv('data/猫眼top100.csv', index=False)


    # get_one_page_data(browser)

    # time.sleep(10)
    browser.quit()
